# -*- coding: utf-8 -*-
import json
import datetime
import requests


def sign(school_id, password,auto_position,vaccination_status):

    url = 'http://ca.zucc.edu.cn/cas/login'
    params = {'service': 'http://yqdj.zucc.edu.cn/feiyan_api/h5/html/daka/daka.html'}
    r = requests.get(url, params, timeout=30)
    cookies = r.cookies.get_dict()
    data = {
        'authType': '0',
        'username': school_id,
        'password': password,
        'lt': '',
        'execution': 'e1s1',
        '_eventId': 'submit',
        'submit': '',
        'randomStr': ''
    }
    url = 'http://ca.zucc.edu.cn/cas/login;jsessionid=' + cookies['JSESSIONID']
    r = requests.post(url, data=data, params=params, cookies=cookies, allow_redirects=False, timeout=30)
    url = r.headers['Location']
    r = requests.get(url, allow_redirects=False, timeout=30)
    cookies = r.cookies.get_dict()


    # 获取问卷
    url = 'http://yqdj.zucc.edu.cn/feiyan_api/examen/examenSchemeController/findExamenSchemeById.do'
    r = requests.post(url, cookies=cookies, data={'esId': 2}, timeout=30)
    questions = json.loads(r.json()['data']['examen']['scheme'])['questions']

    # 填写表单并提交
    with open("./form.json", "r", encoding='utf-8') as f:
        form = json.load(f)
        if form['questions'] != questions:
            return "打卡表单已更新，当前版本不可用"

        answer = form['answer']
        answer["填报日期(Date)"] = str(
            datetime.datetime.now(tz=datetime.timezone(datetime.timedelta(hours=8))).date())
        answer["自动定位(Automatic location)"] = auto_position
        answer["疫苗接种情况?(Vaccination status?)"] = vaccination_status
        data = json.dumps({"examenSchemeId": 2, "examenTitle": "师生报平安", "answer": answer})
        headers = {'Content-Type': 'application/json'}
        url = "http://yqdj.zucc.edu.cn/feiyan_api/examen/examenAnswerController/commitAnswer.do"
        r = requests.post(url, cookies=cookies, data=data, headers=headers, timeout=30)
        res = r.json()
        if res['code'] == 1000:
            return '打卡成功'
        elif res['code'] == 14801:
            return '今日已打卡'
        else:
            return res['message']


if __name__ == '__main__':

    users=[
        {
            'school_id' : '31901085',
            'password' : '015617',
            'auto_position' : '浙江省 杭州市 拱墅区',
            'Vaccination_status' : '已接种第三针( booster jab received)',
        },{
            'school_id' : '31901066',
            'password' : '210922',
            'auto_position' : '浙江省 杭州市 拱墅区',
            'Vaccination_status' : '已接种第三针( booster jab received)',
        },{
            'school_id' : '31901068',
            'password' : '166521',
            'auto_position' : '浙江省 杭州市 拱墅区',
            'Vaccination_status' : '已接种第三针( booster jab received)',
        },{
            'school_id' : '31901069',
            'password' : '292122',
            'auto_position' : '浙江省 杭州市 拱墅区',
            'Vaccination_status' : '已接种第三针( booster jab received)',
        },{
            'school_id' : '31901086',
            'password' : '174318',
            'auto_position' : '浙江省 杭州市 拱墅区',
            'Vaccination_status' : '已接种第一针 (first jab received) ',
        },{
            'school_id' : '31901081',
            'password' : '076938',
            'auto_position' : '浙江省 杭州市 拱墅区',
            'Vaccination_status' : '已接种第三针( booster jab received)',
        },
    ]
    for user in users:
        school_id          = user['school_id']
        password           = user['password']
        auto_position      = user['auto_position']
        Vaccination_status = user['Vaccination_status']
        msg = sign(school_id, password,auto_position,Vaccination_status)
        print(f'{school_id}:{msg}')
